package com.unicomcz.article;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import oracle.sql.CLOB;

import com.unicomcz.ActionBase;

public class ViewArticleAction extends ActionBase
{
    private String artId;
    private String artTitle;
    private String artContent;
    private String operTime;
    
    /**
     * @return the artId
     */
    public String getArtId()
    {
        return artId;
    }

    /**
     * @param artId the artId to set
     */
    public void setArtId(String artId)
    {
        this.artId = artId;
    }

    /**
     * @return the artTitle
     */
    public String getArtTitle()
    {
        return artTitle;
    }

    /**
     * @param artTitle the artTitle to set
     */
    public void setArtTitle(String artTitle)
    {
        this.artTitle = artTitle;
    }

    /**
     * @return the artContent
     */
    public String getArtContent()
    {
        return artContent;
    }

    /**
     * @param artContent the artContent to set
     */
    public void setArtContent(String artContent)
    {
        this.artContent = artContent;
    }

    /**
     * @return the operTime
     */
    public String getOperTime()
    {
        return operTime;
    }

    /**
     * @param operTime the operTime to set
     */
    public void setOperTime(String operTime)
    {
        this.operTime = operTime;
    }

    public String executeAction(){
        log = Logger.getLogger(this.getClass());
        log.debug("开始读取文章"+artId);
        String sqlStr = "SELECT art_title,to_char(oper_time,'yyyy-mm-dd'),art_Content " + 
        		   " FROM dls_news where art_id=? and status=0";
        boolean dbFlag = setupConn("dls");
        if (!dbFlag) {
            log.debug(userInfo.getUserId()+"读取文件失败!因为建立数据库连接失败");
            return "fail";
        }
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sqlStr);
            pstmt.setString(1, artId);
            rs = pstmt.executeQuery();
            if (!rs.next()){
                addActionMessage("文章不存在！");
                return "fail";
            }else{
                log.debug(1);
                artTitle=rs.getString(1);
                operTime=rs.getString(2);
                CLOB clob = (CLOB)rs.getClob(3);        
               artContent = ClobToString(clob);
               //clob.close();
            }
        }catch(Exception e) {
            log.debug(e);
        }finally {
            try {
                rs.close();
            }catch(Exception ee) {
                
            }
        }
        tearDown();
        return SUCCESS;
    }
 // 将字CLOB转成STRING类型
    public String ClobToString(CLOB clob) throws SQLException, IOException {

        String reString = "";
        Reader is = clob.getCharacterStream();// 得到流
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
            sb.append(s);
            s = br.readLine();
        }
        reString = sb.toString();
        return reString;
    }
   protected void regParam2single() {
        try {
            pstmt.setString(1, artId);
        }catch(Exception e) {
            log.info(e);
        }
    }
}
